package com.arron.algorithm.leetcodetop100.链表;

import com.arron.algorithm.leetcodetop100.ListNode;

/**
 *  leetcode 21: 合并有序链表、
 *
 */
public class 合并有序链表 {


    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

        ListNode res = new ListNode(0);

        ListNode temp = res;

        while (list1 !=null && list2 != null){
            if (list1.val > list2.val){
                temp.next = list2;
               list2 = list2.next;
            }else {
                temp.next = list1;
                list1 = list1.next;
            }
            //移动指针
            temp = temp.next;
        }


        if (list1 != null){
            temp.next = list1;
        }


        if (list2 != null){
            temp.next = list2;
        }

        return res.next;
    }


}


